package com.yy.satokenapplication.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yy.satokenapplication.entity.SysBook;
import com.yy.satokenapplication.service.SysBookService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/book")
@RequiredArgsConstructor
public class BookController {
    
    private final SysBookService bookService;
    
    @GetMapping("/list")
    @SaCheckPermission("book:query")
    public Page<SysBook> list(
            @RequestParam(name = "current", defaultValue = "1") Integer current,
            @RequestParam(name = "size", defaultValue = "10") Integer size,
            @RequestParam(name = "title", required = false) String title) {
        
        Page<SysBook> page = new Page<>(current, size);
        LambdaQueryWrapper<SysBook> wrapper = new LambdaQueryWrapper<>();
        if (title != null) {
            wrapper.like(SysBook::getTitle, title);
        }
        return bookService.page(page, wrapper);
    }
    
    @PostMapping("/add")
    @SaCheckPermission("book:add")
    public boolean add(@RequestBody SysBook book) {
        return bookService.save(book);
    }
    
    @PutMapping("/edit")
    @SaCheckPermission("book:edit")
    public boolean edit(@RequestBody SysBook book) {
        return bookService.updateById(book);
    }
    
    @DeleteMapping("/delete/{id}")
    @SaCheckPermission("book:delete")
    public boolean delete(@PathVariable(name = "id") Long id) {
        return bookService.removeById(id);
    }
} 